package com.lin.missyou.repository;

import com.lin.missyou.model.Sku;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author gcq
 * @Create 2021-11-08
 */
@Repository
public interface SkuRepository extends JpaRepository<Sku,Long> {

    public List<Sku> findAllByIdIn(List<Long> ids);

    /**
     * 插入或更改 加上注解 Modifying
     * @param sid
     * @param quantity
     * @return
     */
    @Modifying
    @Query("update Sku s\n" +
            "set s.stock = s.stock - :quantity\n" +
            "where s.id = :sid\n" +
            "and s.stock >= :quantity")
    int reduceStock(Long sid, Long quantity);
}